package org.example;

import org.example.netty.CoordinationNettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
public class Netty implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Netty.class, args);
    }
    private CorsConfiguration corsConfig(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List<String> allowedOrigins = Arrays.asList(
                "http://localhost:8081",
                "http://localhost:8080",
                "http://localhost:9000",
                "http://172.20.10.3:8080",
                "http://8.138.11.218:8080"
        );
        corsConfiguration.setAllowedOrigins(allowedOrigins);
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true); // 明确设置允许凭据
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfig());
        return new CorsFilter(source);
    }
    @Autowired
    private CoordinationNettyServer nettyServer;
    @Override
    public void run(String... args)throws Exception{
        nettyServer.start();
    }
}
